﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using TjZheng.Framework;
using TjZheng.Domain.Business;
using TjZheng.Domain.DataEntity;

namespace TjZheng.Editor
{
    public static class UeditorExtension
    {
        public static MvcHtmlString Ueditor(this HtmlHelper htmlHelper, long imageId, long fileId, string name, string value, object htmlAttributes = null)
        {
            if (name.IsNull())
            {
                return MvcHtmlString.Create("<!--提交到服务器的Name不能为空-->");
            }
            if (imageId < 0 && fileId < 0)
            {
                return MvcHtmlString.Create("<!--imageId并且fileId对应的上传配置不存在-->");
            }
            BizUploadLimit objBizUploadLimit = new BizUploadLimit();
            List<UploadLimit> objUploadLimitList = objBizUploadLimit.GetListIn(string.Join(",", imageId, fileId));
            if (objUploadLimitList.IsNull())
            {
                return MvcHtmlString.Create("<!--未设置上传配置-->");
            }
            IDictionary<string, object> objHtmlAttributes = htmlAttributes.CreateValueDictionary();
            if (objHtmlAttributes.IsNull())
            {
                objHtmlAttributes = new Dictionary<string, object>();
            }
            object style = null;
            if (!objHtmlAttributes.TryGetValue("style", out style))
            {
                objHtmlAttributes["style"] = "width: 900px; height: 800px;";
            }
            StringBuilder objStringBuilder = new StringBuilder();
            string idSelector = Guid.NewGuid().ToString().Replace("-", "");
            objStringBuilder.AppendLine(string.Format("<script id=\"{0}\" type=\"text/plain\" name=\"{1}\" type=\"text/plain\" {2}>{3}</script>", idSelector, name, objHtmlAttributes.Select(keyvalue => string.Format("{0}=\"{1}\"", keyvalue.Key, keyvalue.Value)).JoinString(" "), value));
            objStringBuilder.AppendLine("<script type=\"text/javascript\">");
            string param = idSelector.FormatSelf("ue_{0}");
            objStringBuilder.AppendLine(string.Format("var {0} = UE.getEditor('{1}');", param, idSelector));
            objStringBuilder.AppendLine(param.FormatSelf("{0}.ready(function () {{"));
            objStringBuilder.AppendLine(param.FormatSelf("  {0}.execCommand('serverparam', {{"));
            objStringBuilder.AppendLine(string.Format("     'ImageId': '{0}',", imageId));
            objStringBuilder.AppendLine(string.Format("     'FileId': '{0}',", fileId));
            objStringBuilder.AppendLine("});");
            objStringBuilder.AppendLine("});");
            objStringBuilder.AppendLine("</script>");
            return MvcHtmlString.Create(objStringBuilder.ToString());
        }

        public static MvcHtmlString Ueditor(this HtmlHelper htmlHelper, string imageCategoryCode, string imageLimitCode, string fileCategoryCode, string fileLimitCode, string name, string value, object htmlAttributes = null)
        {
            if (name.IsNull())
            {
                return MvcHtmlString.Create("<!--提交到服务器的Name不能为空-->");
            }
            if (imageCategoryCode.IsNull() && imageLimitCode.IsNull() && fileCategoryCode.IsNull() && fileLimitCode.IsNull())
            {
                return MvcHtmlString.Create("<!--imageLimitCode并且fileLimitCode对应的上传配置不存在-->");
            }
            BizUploadLimit objBizUploadLimit = new BizUploadLimit();
            List<UploadLimit> objUploadLimitList = objBizUploadLimit.GetList(string.Format("(UcCode='{0}' AND LimitCode='{1}') OR ((UcCode='{2}' AND LimitCode='{3}')", imageCategoryCode, imageLimitCode, fileCategoryCode, fileCategoryCode));
            if (objUploadLimitList.IsNull())
            {
                return MvcHtmlString.Create("<!--未设置上传配置-->");
            }
            IDictionary<string, object> objHtmlAttributes = htmlAttributes.CreateValueDictionary();
            if (objHtmlAttributes.IsNull())
            {
                objHtmlAttributes = new Dictionary<string, object>();
            }
            object style = null;
            if (!objHtmlAttributes.TryGetValue("style", out style))
            {
                objHtmlAttributes["style"] = "width: 900px; height: 800px;";
            }
            StringBuilder objStringBuilder = new StringBuilder();
            string idSelector = Guid.NewGuid().ToString().Replace("-", "");
            objStringBuilder.AppendLine(string.Format("<script id=\"{0}\" type=\"text/plain\" name=\"{1}\" type=\"text/plain\" {2}>{3}</script>", idSelector, name, objHtmlAttributes.Select(keyvalue => string.Format("{0}=\"{1}\"", keyvalue.Key, keyvalue.Value)).JoinString(" "), value));
            objStringBuilder.AppendLine("<script type=\"text/javascript\">");
            string param = idSelector.FormatSelf("ue_{0}");
            objStringBuilder.AppendLine(string.Format("var {0} = UE.getEditor('{1}');", param, idSelector));
            objStringBuilder.AppendLine(param.FormatSelf("{0}.ready(function () {{"));
            objStringBuilder.AppendLine(param.FormatSelf("  {0}.execCommand('serverparam', {{"));
            objStringBuilder.AppendLine(string.Format("     'ImageCategoryCode': '{0}',", imageCategoryCode));
            objStringBuilder.AppendLine(string.Format("     'ImageLimitCode': '{0}',", imageLimitCode));
            objStringBuilder.AppendLine(string.Format("     'FileCategoryCode': '{0}',", fileCategoryCode));
            objStringBuilder.AppendLine(string.Format("     'FileLimitCode': '{0}',", fileLimitCode));
            objStringBuilder.AppendLine("});");
            objStringBuilder.AppendLine("});");
            objStringBuilder.AppendLine("</script>");
            return MvcHtmlString.Create(objStringBuilder.ToString());
        }
    }
}
